<template>
    <div>
        <div ref="chartPieRef" class="flex justify-between">
            <div>{{ title }}</div>
            <div>
                right
            </div>
        </div>
        <div class="chart-container" ref="chartRef">
            <button @click="doRandom">Random</button>
        </div>
    </div>
</template>
<script setup>
import { ref, onMounted, onActivated } from 'vue'
import * as echarts from 'echarts'
const props = defineProps({
    title: {
        type: String,
        default: '近七日销售趋势'
    }
})

const chartRef = ref(null)
const chartPieRef = ref(null)
const myChart = ref(null)
const setOption = () => {
    myChart.value.setOption({
        title: {
            text: ''
        },
        tooltip: {
            trigger: 'item'
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 40,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: [
                    { value: 1048, name: 'Search Engine' },
                    { value: 735, name: 'Direct' },
                    { value: 580, name: 'Email' },
                    { value: 484, name: 'Union Ads' },
                    { value: 300, name: 'Video Ads' }
                ]
            }
        ]
    })
}

const resize = () => {
    myChart.value.resize()
}

defineExpose({
    chartPieRef,
    resize
})

onMounted(() => {
    myChart.value = echarts.init(chartRef.value)
    setOption()
})
onActivated(() => {
    myChart.value = echarts.init(chartRef.value)
    setOption()
})
</script>
<style lang="scss" scoped>
.chart-container {
    width: 100%;
    height: 500px;
}
</style>